package com.example.partner.activity;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import com.example.partner.R;
import com.example.partner.common.SharedViewModel;
import com.example.partner.databinding.ActivityMainBinding;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SearchView;
import androidx.appcompat.widget.Toolbar;
import androidx.lifecycle.ViewModelProvider;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;

public class MainActivity extends AppCompatActivity {

    private SharedViewModel sharedViewModel;

    private ActivityMainBinding binding;
    Toolbar toolbar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        sharedViewModel = new ViewModelProvider(this).get(SharedViewModel.class);
        BottomNavigationView navView = findViewById(R.id.nav_view);
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
                R.id.navigation_home, R.id.navigation_team, R.id.navigation_find,R.id.navigation_individual)
                .build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_activity_home);
        NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
        NavigationUI.setupWithNavController(binding.navView, navController);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_toolbar, menu);
        MenuItem searchItem = menu.findItem(R.id.action_search);
        SearchView searchView = (SearchView) searchItem.getActionView();


        searchView.setOnCloseListener(() -> {
            // 用户点击了返回键或关闭了搜索视图
            sharedViewModel.setSearchQuery("");
            return false; // 返回 false 以允许 `SearchView` 处理关闭事件
        });

        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                NavController navController = Navigation.findNavController(MainActivity.this, R.id.nav_host_fragment_activity_home);
                int currentDestinationId = navController.getCurrentDestination().getId();

//                if (currentDestinationId == R.id.navigation_home) {
//                    sharedViewModel.setSearchQuery(query);
//                }
                if (currentDestinationId == R.id.navigation_team) {
                    sharedViewModel.setSearchQuery(query);
                }
                //其他fragment类似

                sharedViewModel.setSearchQuery(query);
                return true;
            }
            @Override
            public boolean onQueryTextChange(String newText) {
                if (newText.isEmpty()) {
                    // 当搜索栏为空时，恢复显示原始数据
                    sharedViewModel.setSearchQuery("");
                } else {
                    // 实时更新搜索结果
                    //performSearch(newText);
                }
                return true;
            }
        });

        return true;
    }





}